package edu.ccut.titan.tag.chart.render;

import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.text.NumberFormat;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;

import edu.ccut.titan.tag.chart.utils.ChartInfo;
import edu.ccut.titan.tag.chart.utils.SubAixs;

/**
 * 曲线图Render
 * 
 * @author LeslieGu
 * 
 */
public class LineChartRender extends ChartRender {

	public LineChartRender() {
	}

	protected JFreeChart createChart(ChartInfo chartInfo) {
		JFreeChart chart = ChartFactory.createLineChart(chartInfo.getTitle(),
				chartInfo.getCategoryAxisLable(), chartInfo
						.getNumberAxisLable(), super
						.getCategoryDataset(chartInfo.getDatas()), chartInfo
						.getOrientation(), chartInfo.isLegend(), chartInfo
						.isTooltips(), chartInfo.isUrls());

		return chart;
	}

	protected Plot renderPrivateChart(JFreeChart chart, ChartInfo chartInfo) {
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setRangeGridlinePaint(Color.lightGray);

		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setDomainGridlinesVisible(true);
		
		// 配置字体
		  Font xlabelfont = new Font("宋体",Font.PLAIN,20) ;// x轴标题字体
		  Font xtickfont = new Font("宋体",Font.PLAIN,15) ;// x轴刻度字体
		  Font ylabelfont = new Font("宋体",Font.PLAIN,20) ;// Y轴标题字体
		  Font ytickfont = new Font("宋体",Font.PLAIN,12) ;// Y轴刻度字体
		  Font titleFont = new Font("宋体", Font.PLAIN , 30) ; // 图片标题
		  Font itemfont = new Font("宋体",Font.PLAIN,15) ;// 图示字体
		     
		  plot.getDomainAxis().setLabelFont(xlabelfont); // x轴标题字体
		  plot.getDomainAxis().setTickLabelFont(xtickfont); // x轴刻度字体
		  if(chartInfo != null && chartInfo.getDatas() != null){
			  int xLabelNumber = chartInfo.getDatas().size();
			  if(xLabelNumber > 50){
				  plot.getDomainAxis().setTickLabelsVisible(false); 
			  }
		  }
		  plot.getRangeAxis().setLabelFont(ylabelfont);  // y轴外围字体
		  plot.getRangeAxis().setTickLabelFont(ytickfont); // y轴标题字体
		  chart.getTitle().setFont(titleFont);   
		 chart.getLegend().setItemFont(itemfont); // 设置最底下方框内的字体（图示）
		 // 抗锯齿关闭
		 chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 

		// customise the range axis...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
		rangeAxis.setAutoRangeIncludesZero(true);
		
		// 纵坐标轴显示整数
		
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		/*
		 * //水平显示纵轴的文字信息 rangeAxis.setAutoRangeIncludesZero(true);
		 * rangeAxis.setUpperMargin(0.20); rangeAxis.setLabelAngle(Math.PI /
		 * 2.0);
		 */
		CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
		categoryAxis.setCategoryLabelPositions(super.getCategoryLablePositions(chartInfo));

		// customise the renderer...
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot
				.getRenderer();
		lineandshaperenderer.setShapesVisible(true);
		lineandshaperenderer.setShapesFilled(true);
		lineandshaperenderer.setDrawOutlines(true);

		renderer.setItemLabelsVisible(true);
		renderer.setPositiveItemLabelPosition(new ItemLabelPosition());
		renderer.setNegativeItemLabelPosition(new ItemLabelPosition());

		showAxisValue(renderer);

		List<SubAixs> subAixses = chartInfo.getSubAixses();
		if (subAixses != null) {
			int i = 1;
			for (SubAixs subAixs : subAixses) {
				this.initialSecondaryAxis(plot, this.getCategoryDataset(subAixs
						.getDatas()), i++, subAixs.getColorPaint(), subAixs
						.getName());
			}
		}

		return plot;
	}

	public void initialSecondaryAxis(CategoryPlot plot,
			CategoryDataset datasetTwo, int i, Paint paint,
			String axisName) {
		NumberAxis numberaxisOne = new NumberAxis(axisName);
		numberaxisOne.setLabelPaint(paint);
		numberaxisOne.setTickLabelPaint(paint);
		plot.setRangeAxis(i, numberaxisOne);
		plot.setDataset(i, datasetTwo);
		plot.mapDatasetToRangeAxis(i, i);
		plot.setRenderer(i, initialRenderShapeAndPaint(paint,
				new LineAndShapeRenderer()));

		showAxisValue((LineAndShapeRenderer) plot.getRenderer(i));
	}

	public LineAndShapeRenderer initialRenderShapeAndPaint(Paint paint,
			LineAndShapeRenderer renderer) {
		renderer.setShapesVisible(true);
		renderer.setShapesFilled(true);
		renderer.setPaint(paint);
		return renderer;
	}

	public void showAxisValue(LineAndShapeRenderer rendererTwo) {
		NumberFormat myFormat = NumberFormat.getNumberInstance();
		myFormat.setMaximumFractionDigits(4);

		// 在StandardCategoryToolTipGenerator中的字符串可以任意设置，其中
		// n：0—RowKey 1—ColumnKey 2—Value 3－％

		rendererTwo.setToolTipGenerator(new StandardCategoryToolTipGenerator(
				"({0}, {1}) 值为 {2}", myFormat));
	}

	public void renderPrivateNakedChart(JFreeChart chart, ChartInfo chartInfo) {
		CategoryPlot categoryPlot = (CategoryPlot) renderPrivateChart(chart,
				chartInfo);
		ValueAxis valueAxis = categoryPlot.getRangeAxis();
		valueAxis.setVisible(false);
	}

	public void renderPrivateNakedDataPart(JFreeChart chart, ChartInfo chartInfo) {
		CategoryPlot categoryPlot = (CategoryPlot) renderPrivateChart(chart,
				chartInfo);
		categoryPlot.setDomainGridlinesVisible(false);
		categoryPlot.setRangeGridlinesVisible(false);
	}

	public void renderPrivateShowDataPart(JFreeChart chart, ChartInfo chartInfo) {
		CategoryPlot categoryPlot = (CategoryPlot) renderPrivateChart(chart,
				chartInfo);
		categoryPlot.setDomainGridlinesVisible(true);
		categoryPlot.setRangeGridlinesVisible(true);
	}
}
